package com.duoketech.service.weixin.activity;

import com.duoketech.exception.ValidateException;
import com.duoketech.model.activity.Activity;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.model.member.Member;
import com.duoketech.model.order.Order;
import com.duoketech.model.room.UnitRoom;
import com.duoketech.model.roomorder.RoomOrder;
import com.duoketech.utils.DateUtils;
import com.duoketech.utils.WxMsgTemplateUtils;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

public class WeiXinActivityService {


    public static final WeiXinActivityService ME = new WeiXinActivityService();


    public List<Activity> queryLastestActivity(int limit ){
        String sql="select t1.*,t2.avatar from "+Activity.dao.tableName()+" t1,"+Member.dao.tableName()+" t2 where t1.status=? and t1.creator=t2.id order by t1.id desc limit ?";
        List<Activity> activities= Activity.dao.find(sql,Activity.STATUS_NORMAL,limit);
        for (Activity a:activities) {
            a.set(Activity.PRICE,a.price());
            a.set(Activity.STATUS,a.status());
        }
        return activities;
    }

    public Page<Activity> queryActivity(int pageNum,int pageSize){
        String sql=" from "+Activity.dao.tableName()+" t1,"+Member.dao.tableName()+" t2 where t1.status=? and t1.creator=t2.id  order by t1.id desc";
        Page<Activity> activityPage=Activity.dao.paginate(pageNum,pageSize,"select t1.*,t2.avatar",sql,Activity.STATUS_NORMAL);
        for (Activity a:activityPage.getList()) {
            a.set(Activity.PRICE,a.price());
            a.set(Activity.STATUS,a.status());
        }
        return activityPage;
    }


    public Page<ActivityOrder> queryActivtyOfMine(int pageNum, int pageSize, int loginId) {
        String sql = " from " + ActivityOrder.dao.tableName() + " t1, "+Activity.dao.tableName()+" t2 where t1.status<>? and t1.member_id=? and t1.obj_id=t2.id order by t1.id desc";
        Page<ActivityOrder> activtyOrder = ActivityOrder.dao.paginate(pageNum, pageSize, "select t1.*,t2.name,t2.desc,t2.start_time,t2.end_time,t2.status as astatus", sql, ActivityOrder.STATUS_DEL, loginId);
        for(ActivityOrder a:activtyOrder.getList()){
            a.put(Activity.START_TIME,DateUtils.formateData(a.getDate(Activity.START_TIME),DateUtils.YYMMDD_FORMAT));
            a.put(Activity.END_TIME,DateUtils.formateData(a.getDate(Activity.END_TIME),DateUtils.YYMMDD_FORMAT));
            a.put("activityStatus",a.status());
            a.put("astatus",Activity.dao.statusConvert(a.getInt("astatus")));
        }
        return activtyOrder;
    }


    public Activity queryActivityById(int id){
        Activity activity = Activity.dao.findById(id);
        activity.set(Activity.PRICE,activity.price());
        activity.set(Activity.START_TIME,activity.startTime());
        activity.set(Activity.END_TIME,activity.endTime());
        return activity;
    }
    public String statusConvert(int code){
        return Activity.dao.statusConvert(code);
    }



    public void cancelActivityOrder(int id,int memberId ) throws ValidateException {
        ActivityOrder activityOrder = ActivityOrder.dao.findById(id);
        if(activityOrder==null){
            throw  new ValidateException("当前记录不存在");
        }

        if(activityOrder.getInt(ActivityOrder.MEMBER_ID)!=memberId){
            throw  new ValidateException("非本人参与的活动，无权限取消");
        }

        if(activityOrder.getInt(ActivityOrder.STATUS)==ActivityOrder.STATUS_CONFIRM){
            throw  new ValidateException("当前活动报名信息已经被确认，无法取消该活动");
        }
        if(activityOrder.getInt(ActivityOrder.STATUS)==ActivityOrder.STATUS_CANCEL){
            throw  new ValidateException("当前活动报名已经被取消了，无法再进行操作");
        }
        activityOrder.set(ActivityOrder.STATUS,ActivityOrder.STATUS_CANCEL);
        activityOrder.set(ActivityOrder.MODIFY_TIME,new Date());
        activityOrder.update();


        Activity activity = Activity.dao.findById(activityOrder.getInt(ActivityOrder.OBJ_ID));

        //向预订人发送消息取消订单提醒
        Member member  = Member.dao.findById(activityOrder.getInt(ActivityOrder.MEMBER_ID));
        WxMsgTemplateUtils.sendOrderCancelMsg(member.getStr(Member.OPEN_ID),
                member.getStr(Member.NICK_NAME),"活动-"+activity.getStr(Activity.NAME),
                "ACT"+member.getInt(Member.ID)+"-"+activityOrder.getInt(ActivityOrder.ID));

        //向发起人发送消息取消订单提醒
        member  = Member.dao.findById(activity.getInt(Activity.CREATOR));
        WxMsgTemplateUtils.sendOrderCancelToOwnerMsg(member.getStr(Member.OPEN_ID),
                member.getStr(Member.NICK_NAME),"活动-"+activity.getStr(Activity.NAME),
                "ACT"+member.getInt(Member.ID)+"-"+activityOrder.getInt(ActivityOrder.ID),
                activityOrder.getStr(ActivityOrder.NAME),activityOrder.getStr(ActivityOrder.MOBILE));
    }


    public ActivityOrder queryActivityByCardNo(String cardNo,int activityId){
        String sql="select * from "+ActivityOrder.dao.tableName()+" where card_no=? and status<>? and obj_id=?";
        return ActivityOrder.dao.findFirst(sql,cardNo,ActivityOrder.STATUS_DEL,activityId);
    }


    public ActivityOrder activityOrderPreview(int id, int memberId) throws ValidateException {
        ActivityOrder activityOrder = ActivityOrder.dao.findById(id);

        if(activityOrder==null){
            throw new ValidateException("当前订单记录不存在");
        }
        if(activityOrder.getInt(RoomOrder.MEMBER_ID)!=memberId){
            throw new ValidateException("非本人预订订单，无法继续操作");
        }
        Activity activity = Activity.dao.findById(activityOrder.getInt(RoomOrder.OBJ_ID));
        activityOrder.put("activity",activity);
        return activityOrder;
    }
}
